﻿
using MainModule.ServiceReference2;
using StudyKitLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainModule
{
    public partial class Product_Move : Form
    {
        ServiceReference2.Container context =
            new ServiceReference2.Container(new Uri("http://localhost:47318/odata"));

        public static RF1356.REPLY reply; // 리턴 상태를 반환하는 변수
        public static long PortNO;

        public Product_Move()
        {
            InitializeComponent();

            var pmp = from kkk in context.Products
                      select ( new
                      {
                          Name=kkk.ProductName,
                          No=kkk.ProductNo,
                      });
            //var pmp = context.Products.Select(kkk => kkk);

            foreach (var kkk in pmp)
            {
                this.cbProductNo.Items.Add(kkk.No);
            }

            cbProductNo.SelectedIndexChanged += (sender, e) =>
            {
               
                var pn = context.Products.Where(kkk => kkk.ProductNo == Convert.ToInt32(cbProductNo.SelectedItem)).SingleOrDefault();

                txtProductName.Text = pn.ProductName;
            };

            


        }
        

        private void btnSubmit_Click(object sender, EventArgs e)
        {

           

            MessageBox.Show("카드 UID : " +Form1.obj + "에 정보가 등록 되었습니다.\n 등록된 정보는 출고처리됩니다.");


            string UID = Form1.obj;
            string DataBlocks = String.Empty;
            string Data = String.Empty;

            DataBlocks = RF1356.str2hex(txtMove.Text);
            reply = RF1356.RFWriteDataBlock(1, UID , ref DataBlocks, 0, 10);


            var InsertProductMove = new ProductMove
            {
                ProductMoveDate = dtpMoveStart.Value,
                ProductMoveQuantity = Convert.ToInt32(txtQuan.Text),
                ProductMoveType = "1",
                ProductMoveId = Convert.ToInt32(txtMove.Text),
                ProductNo = Convert.ToInt32(cbProductNo.SelectedItem.ToString()),
                ProductMoveState = "출고"
            };

            context.AddToProductMoves(InsertProductMove);
            context.SaveChanges();



        }

        private void Product_Move_Load(object sender, EventArgs e)
        {
            txtMove.Enabled = false;
            var kk = context.ProductMoves.OrderBy(kkk => kkk.ProductMoveId).ToList().Max(kkk=>kkk.ProductMoveId);
            txtMove.Text = (kk+1).ToString();
        }

      
    }
}
